import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { ApiService } from '../../../core/api/api.service';
import { MessageService } from '../../../core/service/message.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';

/**
 * 企业管理-新增
 */

@Component({
    templateUrl: './add.html',
})
export class EnterpriseAddComponent implements OnInit {

    validateForm;
    typeList = [{ name: '国有企业', id: 'StateOwned' }]
    industryList = [{ name: '医疗', id: '1' }, { name: '餐饮', id: '2' }, { name: '娱乐', id: '3' }]
    keyLevelList = [{ name: '重点', id: 'Key' }, { name: '非重点', id: 'NonKey' }]
    managerIdList = [{ name: '007', id: '4' }]
    statusList = [{ name: '正常', id: 'Normal' }, { name: '已禁用', id: 'Disabled' }, { name: '待整改', id: 'ToBeRectified' }]
    cityList = [{ name: '南通市', id: '3206' }]
    areaList = []
    // "cityName": "南通市",             // 城市名称 (固定)
    // "areaName": "港闸区",            // 市区编号  (getNextLevel.do 获取)


    ngOnInit() {
        this.validateForm = this.fb.group({
            loginId: [null, [Validators.required]],  // 企业账号
            cityId: [3206, [Validators.required]],  // 城市编号 (固定)
            areaId: [null, [Validators.required]],  // 市区编号 
            address: [null, [Validators.required]],  // 详细地址 
            name: [null, [Validators.required]],  // 企业名称
            orgCode: [null, [Validators.required]],  // 机构代码
            zipcode: [null, [Validators.required]],  // 邮政编码
            type: [null, [Validators.required]],  // 企业性质 StateOwned国有企业
            industryIdList: [null, [Validators.required]],  // 行业分类编号
            legalRepresentative: [null, [Validators.required]],  // 企业法人
            safetyManager: [null, [Validators.required]],  // 安全主管
            contactTel: [null, [Validators.required]],  // 联系电话
            contactEmail: [null, [Validators.required]],  // 联系邮箱
            keyLevel: [null, [Validators.required]],  // 监测级别
            managerId: [null, [Validators.required]],  // 监管员
            homepage: [null],  // 企业网站
            remark: [null],  // 备注
            status: [null, [Validators.required]],  // 状态
            assetTotalCount: [null, [Validators.required]],  // 资产总数
            ipCount: [null, [Validators.required]],  // IP数
            componentCount: [null, [Validators.required]],  // 组件数
            portCount: [null, [Validators.required]],  // 端口数
            vulnTotalCount: [null, [Validators.required]],  // 漏洞总数
            vulnHighRiskCount: [null, [Validators.required]],  // 高危漏洞数
            vulnFixedCount: [null, [Validators.required]],  // 已修复漏洞数
            vulnNotFixedCount: [null, [Validators.required]],  // 未修复漏洞数
        });
        
    }
    // 更换城市
    onChangeCity(cityId) {
        console.log(cityId)
        this.apiService.post('获取各个区', {
            "provinceId": "",
            "cityId": cityId,
            "areaId": ""
        }).then((res: any) => {
            this.areaList = res.data;
        })
    }
    // 提交
    submit(data) {
        if (this.validateForm.valid) {
            // "": "港闸区",            // 市区编号  (getNextLevel.do 获取)
            data.cityName = "南通市";
            for (let i = 0; i < this.areaList.length; i++) {
                const area =  this.areaList[i];
                data['areaId'] == area['id'] ? data.areaName == area['name']:'320601';
            }
            data.areaName = "南通市";
            this.apiService.post('创建企业', data).then((res: any) => {
                this.areaList = res.data;
                this.router.navigate(['/enterprise/list']);
            })
        } else {
            this.nzMessageService.error('请正确填写表单后提交')
        }
    }
    constructor(
        private router: Router,
        private fb: FormBuilder,
        private apiService: ApiService,
        private message: MessageService,
        private nzMessageService: NzMessageService,
        private modal: NzModalService
    ) { }
}
